<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<!doctype html>
<html>
<head>
	<title>汽车产品管理</title>
	<%@ include file="../../common/base.jsp" %>
</head>

<body id="body">
<!--head-->
<div id="head">
	<%@ include file="../../common/head.jsp" %>
</div>
<!--head end-->
<!--container-->
<div id="container">
	<div id="pageId" style="display: none;">05-04</div>
	<!--sidebar-->
	<%@ include file="../../common/menu.jsp" %>
	<!--sidebar-->
	<!--main-->
	<div class="main">
		<div class="location" style="background-position:22px -109px;">
			系统配置<span>&gt;</span>汽车产品管理
		</div>
		<!--content-->
		<div class="content">
			<div id="tableResult">
				<!--权限列表-->
				<div class="listBox">
					<h3>配件列表：<button type="button" class="btn" onClick="spreadAll();" id="spreadAll">全部展开</button>
						<button type="button" class="btn" onClick="showAdd();">添加下级</button>
						<button type="button" class="btn" onClick="showAdd(1);">添加同级</button>
						<button type="button" class="btn" onClick="showUpdate();">修改</button>
					</h3>
					<ul id="rightTree">
					</ul>
				</div>
				<!--权限列表-->
			</div>

		</div>
		<!--content-->

		<!--foot-->
		<jsp:include page="../../common/foot.jsp" />
		<!--foot-->
	</div>
	<!--main-->
</div>
<!--container-->
<script src="${pageContext.request.contextPath}/static/js/basic.js"></script>
<script type="text/javascript">

 	function showAdd(type){
        var id = $("input[name='carproductsId']:checked").val();
 	    if(type == 1){
 	        var parentId = $("#"+id).val();
 	        if(parentId != null ){
                id = parentId;
            }
		}

		if(id == undefined || id== null){
 	        id ="";
		}

        window.location.href="${pageContext.request.contextPath}/carProducts/showAddCarproducts?id="+id;
	}

	function showUpdate(){
        var id = $("input[name='carproductsId']:checked").val();
        if(id == null || id == undefined || id.length < 1){
            prompt.alert("请选择需要修改的汽车产品！");
            return false
		}
        window.location.href="${pageContext.request.contextPath}/carProducts/showUpdateCarproducts?id="+id;
	}



    var TOP_CARPRODUCTSID = "0";

    $(document).ready(function(e) {
        $("#rightTree li").attr("id",TOP_CARPRODUCTSID);
        init();
    });
    //初始化加载渠道列表、权限树相关数据
    function init(parentId){
        $("#tableResult").showLoading();
        $.ajax({
            type: "POST",
            url: "${pageContext.request.contextPath}/carProducts/queryCarproductsTree",
            dataType:"json",
            data:{
                parentId:parentId
            },
            success: function(data){
                var rightData = data.rightData;

                //绑定权限树数据
                for (var j = 0; j < rightData.length; j++) {
                    createTree(rightData[j]);
                }
                $("#tableResult").hideLoading();
            }
        });
    }



    //权限树展开所有
    function spreadAll(){

        if ($("#spreadAll").text()=="全部展开") {
            $("#rightTree ul").show();
            $("#spreadAll").text("全部收缩");
        } else {
            $("#rightTree ul").hide();
            $("#spreadAll").text("全部展开");
        }
    }
    //权限树单个展开收缩
    function spread(clsName){


        $("."+clsName).each(function(i,e){
            if ($(e).is(":hidden")) {
                $(e).show();
            } else {
                $(e).hide();
            }
        });
    }


    //修改时回写权限树中checkbox后父级节点的处理
    function initRightCb(){
        var flag = false;
        $("#rightTree input").each(function(i,e){
            if (flag=$(e).attr("checked")) {
                var menuId = $(e).attr("value");
                var parentMenuId = $("#"+menuId).attr("value");
                dealParent(parentMenuId, flag);
            }
        });
    }

    //权限树checkbox全选
    function selectCheckBox(){
        if ($("#selectAll").attr("checked")) {
            $("#rightTree input").attr("checked",true);
        } else {
            $("#rightTree input").attr("checked",false);
        }
    }

    //权限树单个checkbox处理方法
    function selectCb(menuId,parentMenuId){
        var flag = false;
        //子节点处理
        $("."+menuId+" input").each(function(i,e){//查询子元素下所有checkbox
            if ($("#"+menuId+" input:eq(0)").attr("checked")) {//当前checkbox
                $(e).attr("checked",true);
            } else {
                $(e).attr("checked",false);
            }
        });

        if ($("#"+menuId+" input:eq(0)").attr("checked")) {
            flag = true;
        }
        dealParent(parentMenuId,flag);
    }

    //权限树checkbox父级节点相关处理
    function dealParent(parentMenuId,flag){
        var run = true;
        var mark = true;
        if ($("#"+parentMenuId+" input:eq(0)").length>0) {//检查是否有父节点
            if (!flag) {
                $("."+parentMenuId+" input").each(function(i,e){//检查同级元素是否有选中的
                    if ($(e).attr("checked")) {
                        run = false;
                        return run;
                    }
                });
            }else {
                $("#rightTree input").each(function(i,e){//查询是否全部选中
                    if (!$(e).attr("checked") && $(e).attr("id")!="selectAll") {
                        mark = false;
                        return mark;
                    }
                });
            }
            if (run) {
                $("#"+parentMenuId+" input:eq(0)").attr("checked",flag);
                if (!mark) {
                    $("#selectAll").attr("checked",mark);//取消全选按钮√
                }
                dealParent($("#"+parentMenuId).attr("value"),flag);
            }else {
                $("#selectAll").attr("checked",run);//取消全选按钮√
            }
        }
    }

    //创建权限树
    function createTree(obj){
        var li = "<li id="+obj.id+" value="+obj.parentId+"><input name='carproductsId' type='radio' value='"+obj.id+"'>" +
			"<span style='cursor:pointer;' onclick='spread("+obj.id+");'>"+obj.productsName+"</span></li>";
        if (obj.parentId==TOP_CARPRODUCTSID) {//一级菜单
            $("#rightTree").append(li);
            $("#"+obj.id+" input").attr("class",obj.parentId);
        }else {
            $("#"+obj.parentId).append("<ul class="+obj.parentId+" style='display:none;'>"+li+"</ul>");//找父节点追加元素
        }

        if (obj.SUB_DATA!=undefined) {
            $("#"+obj.id+" span").before("<i class='triangle_l'/>");
            for (var i = 0; i < obj.SUB_DATA.length; i++){
                createTree(obj.SUB_DATA[i]);
            }
        }

    }

</script>
</body>
</html>